package cn.jianyongjie.basic.utils;

import cn.jianyongjie.basic.jwt.JwtUtils;
import cn.jianyongjie.basic.jwt.LoginSuccessObj;
import cn.jianyongjie.basic.jwt.Payload;
import cn.jianyongjie.basic.jwt.RsaUtils;
import cn.jianyongjie.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

/**
* @Description: TODO
* @Author: JianYongJie
* @Version: 1.0
* @Date:  2022/5/17 9:54
*/
public class LoginConText {


    public static Logininfo loginConText(HttpServletRequest request){
        // 从请求头里面获取token
        String token = request.getHeader("U-TOKEN");
        if (token==null){
            // 如果没有值就返回null
            return null;
        }
        try {
            PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("hrm_auth_rsa.pub").getFile());
            // 将泛型改成封装的类
            Payload<LoginSuccessObj> payload = JwtUtils.getInfoFromToken(token, publicKey, LoginSuccessObj.class);
            // 得到返回值
            LoginSuccessObj userInfo = payload.getUserInfo();
            // 获取Logininfo数据
            return userInfo.getLogininfo();
        } catch (Exception e) {
            e.printStackTrace();
            // 报错就返回null
            return null;
        }
    }
}
